package com.cqc.indexbar01;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by a on 2018/3/27.
 * 索引
 * <p>
 * 设置paint
 * 循环drawText()字母
 */

public class IndexBar extends View {

    private Paint paint;
    private int cellWidth;
    private int cellHeight;
    private int height;

    private int touchIndex = -1;


    public static final String[] LETTERS = new String[]{
            "A", "B", "C", "D",
            "E", "F", "G", "H",
            "I", "J", "K", "L",
            "M", "N", "O", "P",
            "Q", "R", "S", "T",
            "U", "V", "W", "X",
            "Y", "Z"};


    public IndexBar(Context context) {
        this(context, null);
    }

    public IndexBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IndexBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        paint.setTypeface(Typeface.DEFAULT_BOLD);
        paint.setTextSize(30);
        paint.setColor(Color.WHITE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        cellWidth = getMeasuredWidth();
        height = getMeasuredHeight();
        cellHeight = height / 26;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        for (int i = 0; i < LETTERS.length; i++) {
            String letter = LETTERS[i];
            float x = cellHeight / 2 - paint.measureText(letter) / 2;

            Rect rect = new Rect();
            paint.getTextBounds(letter, 0, letter.length(), rect);
            float y = cellHeight / 2 + rect.height() / 2 + i * cellHeight;
            canvas.drawText(letter, x, y, paint);
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:

                break;
            case MotionEvent.ACTION_MOVE:

                break;
            case MotionEvent.ACTION_UP:
                touchIndex = (int) (event.getY() / cellHeight);
                if(touchIndex!=-1 && touchIndex<LETTERS.length){
                    if(onClickListener !=null){
                        onClickListener.update(LETTERS[touchIndex]);
                    }
                }
                break;
        }
        return true;
    }


    public interface OnClickListener {
        void update(String letter);
    }

    private OnClickListener onClickListener;

    public void setOnClickListener(OnClickListener onClickListener){
        this.onClickListener = onClickListener;
    }
}
